import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author xhw
 */

@AllArgsConstructor
@Getter
@Setter
@ToString
public class Student implements Comparable<Student> {
    private String name;
    private int score;

    public static void main(String[] args) {
        var students = new ArrayList<Student>();

        students.add(new Student("张三", 80));
        students.add(new Student("李四", 70));
        students.add(new Student("王五", 90));

//        //1. 使用Comparable接口
//        students.sort(Comparator.naturalOrder());
//        System.out.println(students);//按自然排序输出
        List<Student> sort = students.stream().sorted(Comparator.naturalOrder()).toList();
        System.out.println(sort);
//
//        //2. 使用Comparator接口的反向排序
//        students.sort(Comparator.reverseOrder());
//        System.out.println(students);//按自然排序相反的顺序
        List<Student> sort1 = students.stream().sorted(Comparator.reverseOrder()).toList();
        System.out.println(sort1);

//
//        //3.使用自定义比较器
//        students.sort(Comparator.comparing(Student::getName));//按名称升序
        List<Student> sort2 = students.stream().sorted(Comparator.comparing(Student::getName)).toList();
        System.out.println(sort2);

//        //4.使用自定义比较器的反向排序
//        students.sort(Comparator.comparing(Student::getName).reversed());//按名称降序
        List<Student> sort3 = students.stream().sorted(Comparator.comparing(Student::getName).reversed()).toList();
        System.out.println(sort3);

        //上述代码，保持排序逻辑 但是原有集合顺序不变，生成新的集合
    }

    @Override
    public int compareTo(Student another) {
        return Integer.compare(this.score, another.score);
    }
}
